﻿
<div class="box" style="width:600px;">
    <div class="box-title text-left">
        <a href="javascript:$.fancybox.close();" class="pull-right" style="color:#fff;"><i class="fa fa-times"></i></a>
        <empty name='job'>添加招募职位<else />编辑职位信息</empty>
    </div>
    <div class="box-content">
        <form id="formAddJob" method="post">
            <div class="form-horizontal">
                <div class="form-group">
                    <label class="col-xs-2 control-label">职位名称</label>
                    <div class="col-xs-10">
                        <input type="text" name="jobtitle" id="jobtitle" maxlength="20" data-val-max="" class="form-control" data-val="true" data-val-required="请输入职位名称" value="{$job.name}" placeholder="请输入职位名称" />
                        <span data-valmsg-for="jobtitle" data-valmsg-replace="true"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-2 control-label">职位人数</label>
                    <div class="col-xs-3">
                        <if condition="$job['datastate'] egt 10">
                            <select id="jobnum" name="jobnum" disabled="disabled" class="form-control">
                                <option value='10' selected="selected">10</option>
                            </select>
                            <elseif condition="$job['datastate'] gt 0" />
                            <select id="jobnum" name="jobnum" class="form-control">
                                <for start="$job['datastate']" end='11'>
                                    <option value="{$i}" <if condition="$job['num'] eq $i">selected="selected"</if>>{$i}</option>
                                </for>
                            </select>
                            <else />
                            <select id="jobnum" name="jobnum" class="form-control">
                                <for start="1" end='11'>
                                    <option value="{$i}" <if condition="$job['num'] eq $i">selected="selected"</if>>{$i}</option>
                                </for>
                            </select>
                        </if>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-2 control-label">薪酬</label>
                    <div class="col-xs-8">
                        <label><input type="radio" name="salarytype" id="salarytype1" checked /> 单期</label>
                        <label><input type="radio" name="salarytype" id="salarytype2" /> 多期</label>
                        <span class="text-danger">(最多分三期，每期数额不少于总额1/3)</span><br />
                        <div class="form-inline salary-type-item">
                            <label>总&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;额:</label>&nbsp;&nbsp;
                            <input id="salary" name="salary" type="number"
                                   data-val="true" data-val-required="请输入薪酬"
                                   data-val-number="请输入一个有效的数字" min="500"
                                   class="form-control" value="{$job.salary}" placeholder="最小￥500" />
                            <span data-valmsg-for="salary" data-valmsg-replace="true"></span>
                        </div>

                        <div id="phSalaryType2" style="display:none;">
                            <a href="javascript:void(0);" class="button-add-item pull-right"><i class="fa fa-plus"></i> 添加</a>
                            <div class="form-inline salary-type-item">
                                <label>第&nbsp;一&nbsp;期:</label>&nbsp;&nbsp;
                                <input id="salary1" name="salary1" type="number"
                                       data-val="true" data-val-required="请输入薪酬"
                                       data-val-number="请输入一个有效的数字"
                                       data-val-javg="每期数额不少于总额1/3" data-val-javg-value="#salary"
                                       class="form-control" value="{$job.salary2}" /><br />
                                <span data-valmsg-for="salary1" data-valmsg-replace="true" style="margin-left: 65px;"></span>
                            </div>
                            <div id="phSalaries"></div>
                        </div>
                        
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-xs-2 control-label">工作内容</label>
                    <div class="col-xs-10">
                        <textarea type="text" name="zhize" id="zhize" class="form-control" maxlength="200"
                                  data-val="true" data-val-required="请填写工作内容"
                                  rows="6" placeholder="请简要介绍该项目招聘的岗位工作职责及任职要求，200字以内。">
                            {$job.zhize}
                        </textarea>
                        <span data-valmsg-for="zhize" data-valmsg-replace="true"></span>
                    </div>
                </div>
                <div class="form-buttons text-center">
                    <button type="submit" id="btnSubmit" class="btn btn-success"><empty name='job'>新增招募<else />保存信息</empty></button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    require(["jquery", "modules/message"], function ($, messageBox) {

        $.validator.addMethod("javg", function (value, element, params) {
            var t = params['value'];
            var total = parseInt($(t).val());
            if (isNaN(total)) {
                total = 0;
            }
       
            return value >= total / 3;
        }, "此处必填");

        $.validator.unobtrusive.adapters.add("javg", ['value'], function (options) {
            options.rules["javg"] = {
                value: options.params.value
            };
            options.messages["javg"] = options.message;
        });

        var editor;
        $(function () {
           
            $("#formAddJob").removeData("validator");
            $.validator.unobtrusive.parse("#formAddJob");

          
            jQuery.extend(jQuery.validator.messages, { min: jQuery.validator.format("薪酬必须500以上(含500)") });

            var K = window.KindEditor;
            //var editor;
            editor = K.create('textarea[name="zhize"]', {
                resizeType: 1,
                height: 300,
                allowPreviewEmoticons: false,
                allowImageUpload: false,
                items: [
                        'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                        'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                        'insertunorderedlist', '|', 'emoticons', 'image', 'link']
            });
        });

        $("#salarytype1").on("click", function () {
            var isChecked = $("#salarytype1").is(":checked");
            if (isChecked) {
                $("#phSalaryType2").hide();
            } else {
                $("#phSalaryType2").show();
            }
        });

        $("#salarytype2").on("click", function () {
            var isChecked = $("#salarytype2").is(":checked");
            if (isChecked) {
                $("#phSalaryType2").show();
            } else {
                $("#phSalaryType2").hide();
            }
        });

        $("a.button-add-item").on("click", function () {
            var max = 2;
            var lineTexts = ["二", "三"]
            var currentLines = $("#phSalaries .salary-type-item").length;
            if (currentLines < max) {
                var line = $("#tplSalaryLine").clone();
                line.find("label").html("第&nbsp;" + lineTexts[currentLines ] + "&nbsp;期:");
                line.find("input").attr("id", "salary" + (currentLines + 2)).attr("name", "salary" + (currentLines + 2));
                line.find("span").attr("data-valmsg-for", "salary" + (currentLines + 2))
                $("#phSalaries").append(line.html());

                $("#formAddJob").removeData("validator");
                $.validator.unobtrusive.parse("#formAddJob");
            }
        });

        $("#btnSubmit").on("click", function () { 
            if (!$("#formAddJob").valid()) {
                return false;
            }
            editor.sync();
            var url = "{:U('Team/job_ajax')}";
            var pid = parseInt("{$pid|default=-1}");
            var jid = parseInt("{$jid|default=-1}");
            var name = $('#jobtitle').val();
            var salary = $('#salary').val();
            var salary1 = $('#salary1').val();
            var salary2 = $('#salary2').val();
            var num = $("#jobnum option:selected").val();
            var zhize = $('#zhize').val();
            var yaoqiu = $('#yaoqiu').val();

            $.post(url, { pid: pid, id: jid, name: name, salary: salary, num: num, zhize: zhize, yaoqiu: yaoqiu }, function (data) {
                var vdata = eval(data);
                if (vdata.success == 1) {
                    $.fancybox.close();
                    window.location.reload();
                } else {
                    $.fancybox.close();
                    messageBox.alert({ content: vdata.msg });
                }
            }, 'json');
            return false;
        });
    });
</script>

<div style="display:none;" id="tplSalaryLine">
    <div class="form-inline salary-type-item">
        <label>第&nbsp;一&nbsp;期:</label>&nbsp;&nbsp;
        <input id="salary" name="salary" type="number"
               data-val="true" data-val-required="请输入薪酬"
               data-val-number="请输入一个有效的数字"
               data-val-javg="每期数额不少于总额1/3" data-val-javg-value="#salary"
               class="form-control" /><br />
        <span data-valmsg-for="salary1" data-valmsg-replace="true" style="margin-left: 65px;"></span>
    </div>
</div>